package com.sphd.beautysalon.activitys.vip;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.beautysalon.view.DuihuanView;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.adapters.DuiHuanAdapter;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.DuihuanInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.info.VipInfo;
import com.sphd.beautysalon.presenter.DuihuanPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.wzh.frame.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 积分兑换界面
 */
public class DuiJifenActivity extends BaseActivity implements DuihuanView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    DuiHuanAdapter duiHuanAdapter;
    DuihuanPresenterImpl duihuanPresenter;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    VipInfo vipInfo;
    TextView heardView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dui_jifen);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finish());
        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                addData();
            }
        });
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                duihuanNet((DuihuanInfo)baseQuickAdapter.getItem(i));
            }
        });
    }

    private void duihuanNet(DuihuanInfo item) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        alertDialogBuilder
                .setTitle("兑换提示")
                .setMessage("你将兑换" + item.getDishes_name() + "消费" +
                        item.getMember_integral() + "积分")
                .setCancelable(false)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                duiJifenNet(item);
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }

    private void duiJifenNet(DuihuanInfo item) {
        duihuanPresenter.duihuanUrl(RestaurantUrl.Httptitle + RestaurantUrl.VipDuiJifen
                + "cgid=" + item.getCgid()
                + "&memphone=" + vipInfo.getMem_phone()
                + "&memcard=" + vipInfo.getMem_card()
                + "&integral=" + item.getMember_integral()
                + "&dishes=" + StringUtils.utf8Encode(item.getDishes_name()));
    }

    private void initView() {
        setTitleBar(toolbar, "积分兑换");
        intent=getIntent();
        bundle=intent.getExtras();
        restaurantInfo= (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        vipInfo= (VipInfo) bundle.getSerializable(Constant.VIPINFO);
        duihuanPresenter=new DuihuanPresenterImpl(this);
        heardView=new TextView(this);
        heardView.setText("当前积分剩余："+vipInfo.getMem_integral());
        initAdapter();
        addData();
    }

    private void addData() {
        duihuanPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.FindDuiJifen+"cgid="+vipInfo.getMem_cgid()+"&integral="+vipInfo.getMem_integral());
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecy);
        duiHuanAdapter=new DuiHuanAdapter(R.layout.item_duihuan,new ArrayList<>());
        myRecy.setAdapter(duiHuanAdapter);
        duiHuanAdapter.addHeaderView(heardView);
    }

    @Override
    public void recList(List<DuihuanInfo> duihuanInfos) {
        duiHuanAdapter.setNewData(duihuanInfos);
        mySwf.setRefreshing(false);
    }

    @Override
    public void recRes(int res) {
        if (res==1){
            toast("兑换成功");
            setResult(0);
            finish();
        }else {
            toast("兑换失败，请重试");
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
